A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner.
This means that the first item added in the stack will be the last item popped out of the stack.
This means that the last item added to the stack will be the first item popped out of the stack.
the picture above shows the structure of a stack,the top is last node pushed into the stack and the first node that would be popped out, if it’s popped out, the top of a stack will be top.next
pushing a node into a stack will always have a constant time complexity or O(1), because it will always take the same amount of time no matter what
pop also has a constant time complexity, because you will always remove from the top, there is no loops.
it’s always better to check if the stack empty by using isEmpty() method before using pop, so an exception will be raised if there is no nodes to remove in the stack.
Pseudo code for pop
ALGORITHM pop()
// INPUT <-- No input
// OUTPUT <-- value of top Node in stack
// EXCEPTION if stack is empty
Node temp <-- top
top <-- top.next
temp.next <-- null
return temp.value
### Peek Method
peek returns the value inside the top of the stack Typically, you would check isEmpty before conducting a peek. This will ensure that an exception is not raised.
Pseudo code for peek
ALGORITHM peek()
// INPUT <-- none
// OUTPUT <-- value of top Node in stack
// EXCEPTION if stack is empty
return top.value
### isEmpty method
the time complexity for this method is O(1) // constant
Pseudo code for peek
ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean
return top = NULL
*there are two ways for implementing stack in python, which are:
queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first.
FIFO
This means that the first item in the queue will be the first item out of the queue.
LILO
This means that the last item in the queue will be the last item out of the queue.
the following picture shows the structure of a queue
these two methods will always have a constant time complexity because you will always add or remove the first node .
ALGORITHM enqueue(value)
// INPUT <-- value to add to queue (will be wrapped in Node internally)
// OUTPUT <-- none
node = new Node(value)
rear.next <-- node
rear <-- node
ALGORITHM dequeue()
// INPUT <-- none
// OUTPUT <-- value of the removed Node
// EXCEPTION if queue is empty
Node temp <-- front
front <-- front.next
temp.next <-- null
return temp.value
### Peek
When conducting a peek, you will only be inspecting the front Node of the queue.
Typically, you want to check isEmpty before conducting a peek. This will ensure that an exception is not raised.
big O of time for peek is O(1) // constant
ALGORITHM peek()
// INPUT <-- none
// OUTPUT <-- value of the front Node in Queue
// EXCEPTION if Queue is empty
return front.value
Time complexity // O(1)
ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean
return front = NULL
4) In Networks:
a) Queues in routers/ switches
b) Mail Queues
5) Variations